#ifndef TIPDATABASE_HEAD_H
#define TIPDATABASE_HEAD_H

#include "IAlgorithm.h"
#include "TDefAlgorithm.h"

#define PCONSTCHAR  const char * 
#define REDIRECT_MODE_1 0x01
#define REDIRECT_MODE_2 0x02
#include "IAlgorithm.h"
#ifndef WIN32
	#include <iconv.h>
#else
	#include "strCoding.h"
#endif
#include <vector>
#include "Singleton.h"

class TIPDataBase : public Singleton<TIPDataBase>
{
private:
	FILE *						m_fd;
	IAlgorithm *				m_algorithm;
	std::vector< IPLocation *>  m_map_source;
	std::vector< IPLocation *>	m_map_source1;
	char *						m_temp ;
	char						m_buffer [ 1024 ];
	TDefAlgorithm				m_def_algorithm;
public:
	TIPDataBase(void);
	~TIPDataBase(void);
public:
	void			set_algorithm( IAlgorithm * algorithm );
public:
	bool			load_from_file( const char * filename );

	PCONSTCHAR		find_area_name( const DWORD ip );

	void			convert_to_utf8( const char * src , char * utf8 );
private:
	PCONSTCHAR		read_area(DWORD offset);
	BYTE			read_byte();
	DWORD			read_long4( DWORD offset );
	DWORD			read_long3( DWORD offset );
	DWORD			read_long3();
	PCONSTCHAR		read_utf8_string(DWORD offset); 
	PCONSTCHAR		read_string(DWORD offset);
	void			get_ip_location(DWORD offset ,IPLocation * ip) ;
	void			destroy();
private:
	static bool		comare_min( IPLocation * first , IPLocation * second );
	static bool		comare_max( IPLocation * first , IPLocation * second );
};

#endif


